package com.solitaire.chilla.sdk;

import com.solitaire.chilla.sdk.ad.SChillVideoIAP;
import com.solitaire.chilla.sdk.ad.SChillVideoRewardCallback;
import com.solitaire.chilla.sdk.bridge.SChillCocosBridge;
import com.solitaire.chilla.sdk.utils.SChillToastUtil;

import org.json.JSONObject;

public class SChillSDK {

    public static void playVideo(String adSource){
        SChillVideoIAP adService = SChillAdSdk.getAdService();


        if (adService == null) {
            callbackAdError();
            return;
        }

        try {
            adService.showAd(SChillCocosBridge.mainGameActivity.get(), new SChillVideoRewardCallback() {
                @Override
                public void onAdOver() {
                    try {
                        JSONObject jsonObject =getJson(adSource);
                        SChillCocosBridge.getInstance().XSSdkCallback("ad_over_soli", jsonObject.toString());
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }

                @Override
                public void onAdError() {
                    callbackAdError();
                }
            });
        } catch (Exception e) {
            callbackAdError();
        }
    }

    private static JSONObject getJson(String adSource) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("ad_source",adSource);
        }catch (Exception e){
            e.printStackTrace();

        }
        return jsonObject;
    }

    private static void callbackAdError() {
        try {
            SChillCocosBridge.getInstance().XSSdkCallback("ad_error_soli", "{}");
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public static void showToast(final String message) {
        SChillToastUtil.showToast(message);
    }
}

